package com.platform.api;

import cn.hutool.core.lang.Validator;
import com.alibaba.fastjson.JSONObject;
import com.platform.annotation.IgnoreAuth;
import com.platform.annotation.LoginUser;
import com.platform.entity.AddressVo;
import com.platform.entity.UserVo;
import com.platform.service.ApiAddressService;
import com.platform.util.ApiBaseAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者: @author Harmon <br>
 * 时间: 2017-08-11 08:32<br>
 * 描述: ApiIndexController <br>
 */
@RestController
@RequestMapping("/api/address")
public class ApiAddressController extends ApiBaseAction {
    @Autowired
    private ApiAddressService addressService;

    /**
     * 获取用户的收货地址
     */
    @RequestMapping("list")
    public Object list(@LoginUser UserVo loginUser) {
        Map param = new HashMap();
        param.put("user_id", loginUser.getUserId());
        List<AddressVo> addressEntities = addressService.queryList(param);
        return toResponsSuccess(addressEntities);
    }

    /**
     * 获取收货地址的详情
     */
    @RequestMapping("detail")
    public Object detail(@LoginUser UserVo loginUser, @RequestParam Integer id) {

        AddressVo entity = addressService.queryObject(id);
        if (entity == null) {
            return toResponsFail("收货地址不存在");
        }

        Long userId = entity.getUserId();
        if (userId != null && !loginUser.getUserId().equals(userId)) {
            return toResponsFail("不可查看非本账户下的收货地址");
        }

        AddressVo addressVo = addressService.queryObject(id);

        return toResponsSuccess(addressVo);
    }

    /**
     * 添加或更新收货地址
     */
    @RequestMapping("save")
    public Object save(@LoginUser UserVo loginUser, Long id,
                       @RequestParam("userName") String userName,
                       String postalCode,
                       @RequestParam("provinceName") String provinceName,
                       @RequestParam("cityName") String cityName,
                       @RequestParam("countyName") String countyName,
                       @RequestParam("detailInfo") String detailInfo,
                       @RequestParam("telNumber") String telNumber,
                       @RequestParam("is_default") boolean is_default) {


        //常规校验
        if (userName.length() > 20) {
            return toResponsFail("姓名最长20个字符");
        }
        if (detailInfo.length() > 100) {
            return toResponsFail("详细地址最长100个字符");
        }
        if (!Validator.isMobile(telNumber)) {
            return toResponsFail("请输入正确的手机号码");
        }


        AddressVo entity = new AddressVo();
        entity.setId(id);
        entity.setUserId(loginUser.getUserId());
        entity.setUserName(userName);
        entity.setPostalCode(postalCode);
        entity.setProvinceName(provinceName);
        entity.setCityName(cityName);
        entity.setCountyName(countyName);
        entity.setDetailInfo(detailInfo);
        entity.setTelNumber(telNumber);

        if (is_default) {
            //解除之前的所有默认
            addressService.removeDefault();
            entity.setIs_default(1);
        } else {
            entity.setIs_default(0);
        }


        if (null == entity.getId() || entity.getId() == 0) {
            entity.setId(null);
            addressService.save(entity);
        } else {
            addressService.update(entity);
        }

        return toResponsSuccess(entity);
    }

    /**
     * 删除指定的收货地址
     */
    @RequestMapping("delete")
    public Object delete(@LoginUser UserVo loginUser, @RequestParam Integer id) {

        //查询
        AddressVo entity = addressService.queryObject(id);
        if (entity == null) {
            return toResponsFail("收货地址不存在");
        }

        Long userId = entity.getUserId();
        if (userId != null && !loginUser.getUserId().equals(userId)) {
            return toResponsFail("不可删除非本账户下的收货地址");
        }

        addressService.delete(id);
        return toResponsSuccess("");
    }
}